package com.example.administrator.college02.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.college02.BaseFragment;
import com.example.administrator.college02.R;
import com.example.administrator.college02.adapter.CommonAdapter;
import com.example.administrator.college02.adapter.ViewHolder;
import com.example.administrator.college02.model.ItemTaoyuList;
import com.example.administrator.college02.utils.CommonNet;
import com.example.administrator.college02.widget.AppTitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/7/12.
 */
public class TaoyuListFragment extends BaseFragment {

    private String TAG = "TaoyuListFragment";
    private CommonAdapter<ItemTaoyuList> mAdapter;
    private List<ItemTaoyuList> mList;
    @BindView(R.id.lv_taoyu_main) ListView lvTaoyu;
    @BindView(R.id.taou_fragment_pub)
    ImageView imageView;
    @BindView(R.id.app_title_bar)
    AppTitleBar appTitleBar;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.taoyu_fragment,container,false);
        unbinder = ButterKnife.bind(this,view);
        mContext = getContext();
        getDataFromServer();
        mAdapter = new CommonAdapter<ItemTaoyuList>(mContext,R.layout.life_taoyu_item) {
            @Override
            public void conver(ViewHolder holder, ItemTaoyuList itemTaoyuList) {
                holder.setPhotoFromNet(mContext,R.id.taoyu_item_img1, CommonNet.BASE_URL+itemTaoyuList.getTaoyu_goods_thumb());
                holder.setText(R.id.taoyu_item_des,itemTaoyuList.getTaoyu_goods_desc());
                holder.setText(R.id.taoyu_item_price,itemTaoyuList.getTaoyu_goods_price());
            }
        };
        lvTaoyu.setAdapter(mAdapter);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBaseActivity.showFragment(FragmentTaoyuPub.class,"123",null,true);
            }
        });
        appTitleBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fm.popBackStack();
            }
        });
        return view;
    }
    private void getDataFromServer() {
        StringRequest request = new StringRequest(Request.Method.GET, CommonNet.TAOYU_LIST,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        mList = new ArrayList<>();
                        try {
                            JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject object = jsonArray.getJSONObject(i);
                                String student_headerurl = object.getString("student_headerurl");
                                String student_username = object.getString("student_username");
                                String taoyu_goods_time= object.getString("taoyu_goods_time");
                                String taoyu_goods_thumb= object.getString("taoyu_goods_thumb");
                                String taoyu_goods_desc= object.getString("taoyu_goods_desc");
                                String taoyu_goods_price= object.getString("taoyu_goods_price");
                                String taoyu_goods_appoint= object.getString("taoyu_goods_appoint");
                                String taoyu_goods_comment= object.getString("taoyu_goods_comment");
                                com.alibaba.fastjson.JSONArray ja = JSON.parseArray(taoyu_goods_thumb);
                                Log.d(TAG,"---------ja"+ja.toString());
                                if (!ja.getString(0).equals("")){
                                    ItemTaoyuList itemTaoyuList = new ItemTaoyuList(student_headerurl, student_username, taoyu_goods_time, ja.getString(0),taoyu_goods_desc,taoyu_goods_price, taoyu_goods_appoint, taoyu_goods_comment);
                                    mList.add(itemTaoyuList);
                                }
                            }
                            mAdapter.bindData(mList);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        Volley.newRequestQueue(mContext).add(request);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
